package gui;

import java.awt.Color;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;

public class LineChart extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private XYSeries maximum;
	private XYSeries average;
	private XYSeries standardDeviation;

	public LineChart() {
		XYDataset dataset = createDataset();
		JFreeChart chart = createChart(dataset);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 500));
		add(chartPanel);
	}

	/**
	 * Creates a sample dataset 
	 */
	private  XYDataset createDataset() {
		maximum = new XYSeries("Maximum");	
		average = new XYSeries("Average");
		standardDeviation = new XYSeries("Standard Deviation");

		XYSeriesCollection dataset = new XYSeriesCollection();
		dataset.addSeries(maximum);
		dataset.addSeries(average);
		dataset.addSeries(standardDeviation);

		return dataset;
	}
	
	public void addMaxFit(int x, double y) {
		maximum.add(x, y);
	}
	
	public void addAverageFit(int x, double y) {
		average.add(x, y);
	}
	
	public void addStandardDeviation(int x, double y) {
		standardDeviation.add(x, y);
	}

	/**
	 * Creates a chart
	 */
	private JFreeChart createChart(XYDataset dataset) {

		JFreeChart chart = ChartFactory.createXYLineChart("Statistics", "Generation", "Fitness", dataset, PlotOrientation.VERTICAL, true, true, false);

		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		
		return chart;

	}
	
	public void emptyDataSet(){
		maximum.clear(); 
		average.clear(); 
		standardDeviation.clear(); 
	}
	
} 


